<?php

namespace IDock\Insurance\ACIC\Request;

use IDock\Http\Request;
use IDock\Http\RequestMethod;
use IDock\Http\Response;
use IDock\Insurance\ACIC\Response\PolicyPaymentResponse;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Visitor\PolicyPaymentVisitor;

class PolicyPaymentRequest extends AllACICRequest
{
    protected $route = 'vchl-channel/upay/upay.htm';

    protected $method = RequestMethod::GET;

    protected $response = PolicyPaymentResponse::class;

    protected $payDESKey;

    protected $logTip = '支付';

    public $payGateway = '';

    public function loadConfig(array $config): Request
    {
        $this->gateway = $config['gateway'] ?? '';

        $this->logsDir = $config['logsDir'] ?? '';

        $this->timeout = $config['timeout'] ?? '';

        $this->proxy = $config['proxy'];

        $this->payGateway = $config['WAPPayUrl'] ?? '';

        $this->payDESKey = $config['payDESKey'] ?? '';

        return  $this;
    }

    /**
     * @var PolicyPaymentVisitor $visitor
     */
    protected $visitor;

    public function request(Request $request,Response $multiplexResponse = null): Response
    {
        return parent::payRequest($request);
    }

    /**
     * 生成支付链接
     * @return string
     */
    public function getPayUrl(): string
    {
        if (!$this->payGateway) {
            $payGateway = parse_url($this->gateway);

            $host = $payGateway['host'];
            $url = 'http://'.$host . '/' . $this->route;
            $paymentUrl =  $url . '?' . http_build_query($this->generate());

        } else {
            $paymentUrl =  $this->payGateway . '?' . http_build_query($this->generate());
        }

        return $paymentUrl;
    }

    public function generate(): array
    {
        $param = [
            'CConfirmNo' => $this->visitor->ciInquiryNo,
            'BConfirmNo' => $this->visitor->biInquiryNo,
        ];

        //前端支付类型
        //102001	安诚微信APP支付
        //202001	银联聚合支付
        //302001	支付宝
        //40201	微信支付
        //$param['AppFinRpType'] = '102001';

        foreach ($param as $k => $v) {
            if (!$v) {
                unset($param[$k]);
            }
        }

        if ($this->visitor->nonCarInquirySingle) {
            /*** @var NonCarInquirySinglePrinter $nonCarSingle */
            $nonCarSingle = $this->visitor->nonCarInquirySingle[0];
            //非车合并缴费加密金额，使用DES加密的base64url编码串
            $param['NVhlAmt'] = $this->DESEncrypt($nonCarSingle->premium);
            //非车产品信息描述， 中文使用utf-8的base64url编码,其格式如下：子产品描述,金额,投保单号,产品号，保险起期(格式：yyyyMMddHHmmss)[;子产品描述1,金额1,投保单号1,产品号1，保险起期]
            $param['NVhlDesc'] = base64_encode(implode(',',[
                '驾意险',
                $nonCarSingle->premium * $nonCarSingle->insuredNum,
                $nonCarSingle->inquiryNo,
                '0676',
                date('YmdHis',strtotime($this->visitor->argc['biBeginDate']))
            ]));
            //非车合并缴费操作标志
            //0：开启合并缴费（不传默认为该值）
            $param['NVhlFlag'] = '0';
        }

        return $param;
    }

    private function DESEncrypt($amount)
    {
        return openssl_encrypt($amount,'DES-ECB',$this->payDESKey,0,'');
    }
}
